package com.ghost.UI
{
	import com.ghost.model.UI_skin_data;
	
	import fl.transitions.TransitionManager;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
	
	import spark.primitives.Rect;

	/**
	 * @author ghost
	 * combobox组件
	 */
	public class Ghost_combobox extends Sprite
	{
		private var bg_o:Bitmap;//背景
		private var txt:Ghost_txt;//文本
		private var text_formate:TextFormat;//文本表现格式
		private var bg_bitmap:*;//下拉mc的背景
		private var txt_bg:*;//文本的背景
		private var change_btn_skin_v:Vector.<Object>;//改变按钮皮肤
		private var data_mc:Sprite;//存放数据的容器  
		private var data_mc_visible:Boolean;//数据容器是否可见
		private var data_mc_scroll_rec:Rectangle;//数据容器的可视范围
		private var scroll_mc:Ghost_mc_scroll;//滚动条
		public function Ghost_combobox(txt_width:Number,txt_height:Number,textformate:TextFormat=null)
		{
			data_mc = new Sprite();
			this.addChild(data_mc);
			data_mc.visible = false;
			bg_bitmap = UI_skin_data.ghost_combobox_skin_v[0];
			txt_bg = UI_skin_data.ghost_combobox_skin_v[1];
			change_btn_skin_v = UI_skin_data.ghost_combobox_skin_v.slice(2);
			if(bg_bitmap)
			{
				bg_o = new bg_bitmap() as Bitmap;
				this.addChild(bg_o);
				bg_o.visible = false;
			}
			if(txt_bg)
			{
				var txt_bg_mc:Bitmap = new txt_bg() as Bitmap;
				this.addChild(txt_bg_mc);
				bg_o.y = txt_bg_mc.height;
				txt_bg_mc.width = txt_width;
				txt_bg_mc.height = txt_height;
			}
			txt = new Ghost_txt(txt_width,txt_height,null,true);
			txt.selectable = false;
			txt.addEventListener(MouseEvent.CLICK,txt_cHandler);
			this.addChild(txt);
			if(textformate)text_formate = textformate;
			else text_formate = new TextFormat(null,12,0x000000);
			
			var btn:Ghost_Btn = new Ghost_Btn(change_btn_skin_v,open_close_Handler);
			this.addChild(btn);
			btn.x = txt_width;
			data_mc.y = txt_height;
			this.addEventListener(Event.REMOVED_FROM_STAGE,remove_Handler);
		}
		private function txt_cHandler(e:MouseEvent):void
		{
			open_close_Handler(e);
		}
		//点击下拉按钮
		private function open_close_Handler(e:MouseEvent):void
		{
			scroll_mc.visible = data_mc.visible = data_mc_visible = !data_mc_visible;
		}
		private function remove_Handler(e:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,remove_Handler);
		}
		/**
		 * @param 设置初始提示值
		 */		
		public function set prompt(str:String):void
		{
			txt.text = str;
			txt.setTextFormate(text_formate);
		}
		/**
		 * @param rec 数据容器可视范围
		 */		
		public function set setscrollrec(rec:Rectangle):void
		{
			data_mc_scroll_rec = rec;
			if(rec.height<data_mc.height)
			{
				data_mc.scrollRect = data_mc_scroll_rec;
				scroll_mc = new Ghost_mc_scroll(UI_skin_data.ghost_scroll_skin_v);
				scroll_mc.target = data_mc;
				scroll_mc.move_to(data_mc.x+data_mc.width,data_mc.y);
				scroll_mc.visible = false;
				this.addChild(scroll_mc);
			}
		}
		/**
		 * @param data_arr cell数据
		 * @param cell_cl cell的类
		 ** 表现数据
		 */		
		public function set_data(data_arr:Array,cell_cl:Class,f_bool:Boolean = false):void
		{
			var l:int = data_arr.length;//数组个数
			var i:int = 0;
			var cell_mc:*;
			for(;i<l;i++)
			{
				cell_mc = new cell_cl();
				cell_mc.setsize(100,20);
				cell_mc.label = "label"+String(i);
				if(f_bool)cell_mc.init(data_arr[i],call_back);
				cell_mc.y = i*(cell_mc.height+1);
				data_mc.addChild(cell_mc);
			}
		}
		//点击cell回调
		private function call_back(str:String):void
		{
			txt.text = str;
		}
	}
}